package fgoScript.entity.panel;

import alarm.ZLable;
import alarm.ZTextField;
import alarm.imp.ZtextDo;
import aoshiScript.entity.IWuNa;
import com.melloware.jintellitype.JIntellitype;
import commons.entity.Constant;
import commons.entity.NativeCp;
import commons.util.GameUtil;
import commons.util.ProcessDealUtil;
import commons.util.PropertiesUtil;
import fgoScript.entity.BaseZButton;
import fgoScript.entity.Zpanel;
import fgoScript.entity.panel.inte.PanelModel;
import fgoScript.exception.AppNeedStopException;
import gudaziEx.entity.*;
import gudaziEx.service.ClickManage;
import gudaziEx.util.ChainUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.swing.*;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SetupClickPanel extends Zpanel implements PanelModel {
    private static final Logger LOGGER = LogManager.getLogger(SetupClickPanel.class);
    private IWuNa wuna;
    private Point pFrom;
    private Point pTo;

    public Point getpFrom() {
        return pFrom;
    }

    public void setpFrom(Point pFrom) {
        this.pFrom = pFrom;
    }

    public Point getpTo() {
        return pTo;
    }

    public void setpTo(Point pTo) {
        this.pTo = pTo;
    }

    public void setWuna(IWuNa wuna) {
        this.wuna = wuna;
    }

    public SetupClickPanel() {
        initP();
    }


    @Override
    public void initP() {
        int len = 2;
        Zpanel northPanel = new Zpanel();
//        Zpanel onePanel = new Zpanel();



        //点击设置
        BaseZButton button01 = new BaseZButton(null, 0, "点击设置", JIntellitype.MOD_ALT, KeyEvent.VK_1, false, false, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;

            @Override
            public void runMethod() {

                wuna.configClick(this);
            }
        };
        //清空
        BaseZButton button02 = new BaseZButton(null, 0, "重置", JIntellitype.MOD_SHIFT, (int) 'P', true, true, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;

            @Override
            public void runMethod() {
                wuna.falshClick(this);
            }
        };
        northPanel.add(button01);
        northPanel.add(button02);
        //设置子panel属性
        northPanel.setLayout(new GridLayout(1, 3));
        northPanel.setBounds(5, 5 + (13 * (30+len)), 320, 25+len);
        northPanel.setOpaque(false);
        northPanel.setName("bottomButtons");
        this.add(northPanel);

        Zpanel shundianPanel = new Zpanel();
        //点击设置
        BaseZButton sdButton01 = new BaseZButton(null, 0, "开始设置", JIntellitype.MOD_ALT, KeyEvent.VK_3, false, false, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;

            @Override
            public void runMethod() {
                configPoints(this);
//                wuna.configClick(this);
            }
        };

        //清空
        BaseZButton sdButton02 = new BaseZButton(null, 0, "重置", JIntellitype.MOD_SHIFT, (int) 'P', true, true, BaseZButton.pink) {
            private static final long serialVersionUID = 6504858991507730448L;

            @Override
            public void runMethod() {
                String name = PropertiesUtil.getValueFromSDFile("SDName");
                String filepath = System.getProperty("user.dir") + "/config/" + Constant.MOVE_POINTS+ "/" ;
                File directory = new File(filepath);
                MovePoints mps = new MovePoints();
                mps.setMovePoints(new ArrayList<Point>());
                mps.setName(name);
                File file = new File(directory, name.trim());
                file.delete();
                ChainUtil.saveMovePoints(mps);
            }
        };
        shundianPanel.add(sdButton01);
        shundianPanel.add(sdButton02);
        //设置子panel属性
        shundianPanel.setLayout(new GridLayout(1, 3));
        shundianPanel.setBounds(5, 5 + (2 * (30+len)), 320, 25+len);
        shundianPanel.setOpaque(false);
        shundianPanel.setName("bottomButtons");
        this.add(shundianPanel);

        //
        BaseZButton sdButton03 = new BaseZButton(null, 0, "续点按键(Alt+D)", JIntellitype.MOD_ALT, (int) 'D', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;

            @Override
            public void runMethod() {
                try {
                    movePointsClick();
                } catch (AppNeedStopException e) {
                    e.printStackTrace();
                }
            }
        };
        sdButton03.setBounds(5, 5 + ((3) * (30+len)), 210, 25+len);
        sdButton03.setAllowRepeat(true);
        this.add(sdButton03);

        //按键倍率按钮
        ZTextField sdZf2 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "sdMultiFactor"));
        sdZf2.setBounds(275, 5 + (3 * (30+len)), 50, 25+len);
        this.add(sdZf2);


        //自动按键列表
        JComboBox sdJb = getSdComboBox();
        sdJb.setBounds(5, 5 + (1 * (30+len)), 320, 25+len);
        this.add(sdJb);

        //
        BaseZButton button03 = new BaseZButton(null, 0, "自动按键(Alt+X/1)", JIntellitype.MOD_ALT, (int) 'X', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;

            @Override
            public void runMethod() {
                wuna.alwaysClick();
            }
        };
        button03.setBounds(5, 5 + ((12) * (30+len)), 210, 25+len);
        button03.setAllowRepeat(true);
        this.add(button03);

        //按键类型按钮
        List<String> changeList = new ArrayList<>();
        changeList.add("左键");
        changeList.add("判断");
        changeList.add("按住");
        BaseZButton button04 = BaseZButton.getChangeListButton(changeList,"changeButton_" + NativeCp.getUserName(),"", "clickStrategy", true, true, BaseZButton.pink);
        button04.setBounds(220, 5 + (12 * (30+len)), 50, 25+len);
        this.add(button04);

        //按键倍率按钮
        ZTextField zf2 = new ZTextField(new ZtextDo("0", "changeButton_" + NativeCp.getUserName(),"", "multiFactor"));
        zf2.setBounds(275, 5 + (12 * (30+len)), 50, 25+len);
        this.add(zf2);

        //自动按键列表
        JComboBox jb = getScriptComboBox();
        jb.setBounds(5, 5 + (11 * (30+len)), 320, 25+len);
        this.add(jb);
        //labe1
        ZTextField textField = new ZTextField();
        textField.setBounds(105, 5 + ((10) * (30+len)), 217, 25+len);

        //
        BaseZButton button05 = new BaseZButton(null, 0, "新建配置", JIntellitype.MOD_SHIFT, (int) 'O', true, false, BaseZButton.pink) {
            private static final long serialVersionUID = 432750026975346042L;

            @Override
            public void runMethod() throws Exception {
                String path = System.getProperty("user.dir") + "/config/CK/";
                String text = textField.getText();
                if(GameUtil.isBlank(text)){
                    ProcessDealUtil.displayTray("未命名","文件名为空", TrayIcon.MessageType.WARNING);
                    return;
                }
                GameUtil.createUniqueFile(path,text);
            }
        };
        button05.setBounds(5, 5 + ((10) * (30+len)), 100, 25+len);
        button05.setAllowRepeat(true);
        this.add(button05);
        this.add(textField);

        ZLable lable02 = new ZLable("顺点文件名");
        lable02.setHorizontalAlignment(SwingConstants.CENTER);
        lable02.setBounds(5, 5 + ((0) * (30+len)), 100, 25+len);
        this.add(lable02);
        //labe1
        ZTextField textField05 = new ZTextField();
        textField05.setBounds(105, 5 + ((0) * (30+len)), 153, 25+len);
        this.add(textField05);
        //
        BaseZButton button10 = new BaseZButton(null, 0, "生成", JIntellitype.MOD_SHIFT, (int) 'O', true, true, BaseZButton.pink) {
            private static final long serialVersionUID = 432750026975346042L;

            @Override
            public void runMethod() throws Exception {
                MovePoints mps = new MovePoints();
                String text = textField05.getText();
                mps.setMovePoints(new ArrayList<Point>());
                mps.setName(text);
                if(GameUtil.isBlank(text)){
                    ProcessDealUtil.displayTray("未命名","文件名为空", TrayIcon.MessageType.WARNING);
                    return;
                }
                ChainUtil.saveMovePoints(mps);
            }
        };
        button10.setBounds(258, 5 + ((0) * (30+len)), 64, 25+len);
        button10.setAllowRepeat(true);
        this.add(button10);


        ZLable lable01 = new ZLable("移动文件名");
        lable01.setHorizontalAlignment(SwingConstants.CENTER);
        lable01.setBounds(5, 5 + ((6) * (30+len)), 100, 25+len);
        this.add(lable01);
        //labe1
        ZTextField textField02 = new ZTextField();
        textField02.setBounds(105, 5 + ((6) * (30+len)), 217, 25+len);
        this.add(textField02);
        //labe1
        ZTextField textField03 = new ZTextField();
        textField03.setBounds(65, 5 + ((7) * (30+len)), 60, 25+len);
        BaseZButton button06 = new BaseZButton(null, 0, "Alt+3", JIntellitype.MOD_ALT, (int) '3', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;

            @Override
            public void runMethod() {
               Point p = GameUtil.getMousePosition();
               setpFrom(p);
                textField03.setText((int) p.getX()+","+(int) p.getY());
            }
        };
        button06.setBounds(5, 5 + ((7) * (30+len)), 60, 25+len);
        button06.setAllowRepeat(true);
        this.add(button06);
        this.add(textField03);

        //labe1
        ZTextField textField04 = new ZTextField();
        textField04.setBounds(187, 5 + ((7) * (30+len)), 70, 25+len);
        BaseZButton button07 = new BaseZButton(null, 0, "Alt+4", JIntellitype.MOD_ALT, (int) '4', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;

            @Override
            public void runMethod() {
                Point p = GameUtil.getMousePosition();
                setpTo(p);
                textField04.setText((int) p.getX()+","+(int) p.getY());
            }
        };
        button07.setBounds(127, 5 + ((7) * (30+len)), 60, 25+len);
        button07.setAllowRepeat(true);
        this.add(button07);

        this.add(textField04);

        //
        BaseZButton button08 = new BaseZButton(null, 0, "生成", JIntellitype.MOD_SHIFT, (int) 'O', true, true, BaseZButton.pink) {
            private static final long serialVersionUID = 432750026975346042L;

            @Override
            public void runMethod() throws Exception {
                MoveThings mt = new MoveThings();
                mt.setFromPoint(pFrom);
                mt.setToPoint(pTo);
                String text = textField02.getText();
                if(GameUtil.isBlank(text)){
                    ProcessDealUtil.displayTray("未命名","文件名为空", TrayIcon.MessageType.WARNING);
                    return;
                }
                ChainUtil.saveMoveThings(mt,text);
            }
        };
        button08.setBounds(258, 5 + ((7) * (30+len)), 64, 25+len);
        button08.setAllowRepeat(true);
        this.add(button08);

        BaseZButton button09 = new BaseZButton(null, 0, "移动（Alt+5）", JIntellitype.MOD_ALT, (int) '5', false, false, BaseZButton.pink) {
            private static final long serialVersionUID = -1900866258318497377L;

            @Override
            public void runMethod() {
                MoveThings mt = ChainUtil.getMoveThingsChain(PropertiesUtil.getValueFromMOVEFile("MOVEName"));
                try {
                    GameUtil.moveToDestinyPoint(mt.getFromPoint(),mt.getToPoint());
                } catch (AppNeedStopException e) {
                    e.printStackTrace();
                }
            }
        };
        button09.setBounds(5, 5 + ((8) * (30+len)), 100, 25+len);
        button09.setAllowRepeat(true);
        this.add(button09);

        //
        JComboBox jbMove = getMoveScriptComboBox();
        jbMove.setBounds(105, 5 + ((8) * (30+len)), 217, 25+len);
        this.add(jbMove);
        //
        //
        //设置子panel属性
//        onePanel.setLayout(new GridLayout(1, 3));
//        onePanel.setBounds(5, 5 + (13 * (30+len)), 320, 25+len);
//        onePanel.setOpaque(false);
//        this.add(onePanel);
        this.setOpaque(false);
        this.setLayout(null);
        this.setVisible(false);
        this.setBounds(0, 0, 335, 518);
    }
    private void configPoints(BaseZButton bt){
        String text = bt.getText();
        if ("开始设置".equals(text)) {
            bt.setText("设置续点（alt3）");
        } else if ("设置续点（alt3）".equals(text)){
            MovePoints mps = MovePoints.getInstance();

            List<Point> points = mps.getMovePoints();
            points.add(GameUtil.getMousePosition());
            mps.setMovePoints(points);
            ChainUtil.saveMovePoints(mps);
        }
    }
    private void movePointsClick() throws AppNeedStopException {
        MovePoints mps = MovePoints.getInstance();
        List<Point> points = mps.getMovePoints();
        int factor = ClickManage.getFactorForSdMultiFactor();
        for (Point point:points) {
            GameUtil.getMymouse().mouseMoveByPoint(point, false,false);
            GameUtil.delay(factor);
            GameUtil.getMymouse().mousePressAndRelease(50);
            GameUtil.delay(factor);
        }

    }
    private JComboBox getScriptComboBox(){
        JComboBox jb = new JComboBox();
        addItemsFromFile(jb);
        jb.setUI(new MyComboBoxUI());
        jb.setRenderer(new MyListCellRenderer());
        jb.setEnabled(true);
        jb.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                jb.removeAllItems();
                addItemsFromFile(jb);
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                Map<String, String> map = new HashMap<>();
                String ckName = jb.getSelectedItem().toString();
                map.put("CKName",ckName);
                PropertiesUtil.setValueForCK(map);
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }
        });
        return jb;
    }
    private JComboBox getSdComboBox(){
        JComboBox jb = new JComboBox();
        addItemsFromSdFile(jb);
        jb.setUI(new MyComboBoxUI());
        jb.setRenderer(new MyListCellRenderer());
        jb.setEnabled(true);
        jb.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                jb.removeAllItems();
                addItemsFromSdFile(jb);
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                Map<String, String> map = new HashMap<>();
                String sdName = jb.getSelectedItem().toString();
                map.put("SDName",sdName);
                PropertiesUtil.setValueForSD(map);
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }
        });
        return jb;
    }
    private void addItemsFromSdFile(JComboBox jb){
        String name = PropertiesUtil.getValueFromSDFile("SDName");
        String filepath = NativeCp.getUserDir() + "/config/" + Constant.MOVE_POINTS;
        File file = new File(filepath);
        if (file.exists() ){
            File[] files =  file.listFiles();
            int size = files.length;
            File tempFile;
            int j = 0;
            for (int i = 0; i < size; i++) {
                tempFile = files[i];
                if (tempFile.isFile()){
                    jb.addItem(tempFile.getName());
                    if (name!=null && name.equals(tempFile.getName())){
                        jb.setSelectedIndex(j);
                    }
                    j++;
                }
            }
        }else{
            file.mkdirs();
        }

    }
    private void addItemsFromFile(JComboBox jb){
        String name = PropertiesUtil.getValueFromCKFile("CKName");
        String filepath = NativeCp.getUserDir() + "/config/" + Constant.CK;
        File file = new File(filepath);
        if (file.exists() ){
            File[] files =  file.listFiles();
            int size = files.length;
            File tempFile;
            int j = 0;
            for (int i = 0; i < size; i++) {
                tempFile = files[i];
                if (tempFile.isFile()){
                    jb.addItem(tempFile.getName());
                    if (name!=null && name.equals(tempFile.getName())){
                        jb.setSelectedIndex(j);
                    }
                    j++;
                }
            }
        }else{
            file.mkdirs();
        }

    }
    private JComboBox getMoveScriptComboBox(){
        JComboBox jb = new JComboBox();
        addItemsFromMoveFile(jb);
        jb.setUI(new MyComboBoxUI());
        jb.setRenderer(new MyListCellRenderer());
        jb.setEnabled(true);
        jb.addPopupMenuListener(new PopupMenuListener() {
            @Override
            public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
                jb.removeAllItems();
                addItemsFromMoveFile(jb);
            }

            @Override
            public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
                Map<String, String> map = new HashMap<>();
                String moveName = jb.getSelectedItem().toString();
                map.put("MOVEName",moveName);
                PropertiesUtil.setValueForMove(map);
            }

            @Override
            public void popupMenuCanceled(PopupMenuEvent e) {
            }
        });
        return jb;
    }
    private void addItemsFromMoveFile(JComboBox jb){
        String name = PropertiesUtil.getValueFromMOVEFile("MOVEName");
        String filepath = NativeCp.getUserDir() + "/config/" + Constant.MOVE;
        File file = new File(filepath);
        if (file.exists() ){
            File[] files =  file.listFiles();
            int size = files.length;
            File tempFile;
            int j = 0;
            for (int i = 0; i < size; i++) {
                tempFile = files[i];
                if (tempFile.isFile()){
                    jb.addItem(tempFile.getName());
                    if (name!=null && name.equals(tempFile.getName())){
                        jb.setSelectedIndex(j);
                    }
                    j++;
                }
            }
        }else{
            file.mkdirs();
        }

    }
}
